// pages/index/index.js
Page({

    /*  页面的初始数据 */
    data: {
      //当前选中的选项
      datalist: [],
      index: 0,
      navlist:["美女壁纸","汽车壁纸","游戏壁纸","人物明星","动画壁纸","迪丽热巴","古力娜扎","高清壁纸","搞笑萌宠","时尚搭配","美妆造型","婚纱婚礼","文字句子","插画绘画","风景山水"," 影音书籍","人文艺术","设计","古风","壁纸","旅行","头像","摄影","表情","素材","动图"],
      word: "美女壁纸"
    },

    // 点击跳转详情
    imgclick:function(e){
      const index=e.currentTarget.dataset.index
      const imgdata= this.data.datalist[index]

      const dataobj={
        url:imgdata.photo.path,
        title:imgdata.msg,
        time:imgdata.add_datetime_pretty,
        username:imgdata.sender.username,
        userpic:imgdata.sender.avatar
      }
      // 跳转
      wx.navigateTo({
        url: '../detail/detail',
        success: (res) => {
          res.eventChannel.emit('acceptDataFromOpenerPage', { data: dataobj })
        },
      })
    },

    /* 生命周期函数--监听页面加载: 页面加载时调用 */
    onLoad(options) {
      wx.setNavigationBarTitle({
        title: '壁纸分类',
      })
      //默认调用美女壁纸
      this.getdata()
    },

    // 渲染数据
    getdata:function () {
      // 取到关键词
      const word= this.data.word
      /* 将当前的this对象复制一份给that变量，因为this是不断变化的  */ 
      const that = this
      wx.request({
        /* url是堆糖搜索时的请求接口url，关键词是kw和after_id */ 
        url: 'https://www.duitang.com/napi/blogv2/list/by_search/?kw='+ word +'&after_id=0&type=feed&include_fields=top_comments%2Cis_root%2Csource_link%2Citem%2Cbuyable%2Croot_id%2Cstatus%2Clike_count%2Clike_id%2Csender%2Calbum%2Creply_count%2Cfavorite_blog_id&_type=&_=1653729914442',
        success: (result) => {
          // console.log(result.data.data.object_list)
          that.setData({
            datalist: result.data.data.object_list,
          })
        },
      })
    },

    // 点击导航触发的函数
    navclick: function(e) {
      this.setData({
        word: e.currentTarget.dataset.name,
        index: e.currentTarget.dataset.index,
      })
      this.getdata()
    },
})